package com.yanqu.road.dao.impl.activity.xingyoushanshui.cross;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.youxing.CrossXingYouShanShuiTeamActionLogData;
import com.yanqu.road.utils.date.DateHelper;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossXingYouShanShuiTeamActionLogDataDaoImpl extends TempDataObjectDao<CrossXingYouShanShuiTeamActionLogData> {

    public long getMaxId(int activityId, int groupId) {
        String sql = "select max(id) as maxId from t_c_xing_you_team_action_log_data where activityId=? and groupId = ?";
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            call.setInt(i++,groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                long data = rs.getLong("maxId");
                if(data == 0){
                    return 1;
                }
                return data + 1;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return 1;
    }

    public static String getCrossXingYouShanShuiTeamActionLogDataSql = "select * from `t_c_xing_you_team_action_log_data` where `activityId`=?  and groupId = ? and logTime > ? order by logTime desc";
    public Map<Integer, List<CrossXingYouShanShuiTeamActionLogData>> getCrossXingYouShanShuiTeamActionLogData(int activityId, int groupId, int limit) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<CrossXingYouShanShuiTeamActionLogData>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossXingYouShanShuiTeamActionLogDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            call.setLong(i++, DateHelper.getTodayZeroTimeStamp());
            rs = call.executeQuery();
            while (rs.next()) {
                CrossXingYouShanShuiTeamActionLogData logData = parse(rs);
                if(!result.containsKey(logData.getTeamId())){
                    result.put(logData.getTeamId(), new ArrayList<>());
                }
                List<CrossXingYouShanShuiTeamActionLogData> list = result.get(logData.getTeamId());
                if(list.size() >= limit){
                    continue;
                }
                list.add(logData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateCrossXingYouShanShuiTeamActionLogDataSql = "update `t_c_xing_you_team_action_log_data` set teamId=?,logType=?,userId=?,userId2=?,param=?,reward=?,logTime=?,eventId=?,drift=? where activityId=? and groupId = ? and id=?";
    @Override
    protected boolean updateData(CrossXingYouShanShuiTeamActionLogData crossXingYouShanShuiTeamActionLogData) {
        crossXingYouShanShuiTeamActionLogData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamActionLogData.getTeamId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamActionLogData.getLogType()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiTeamActionLogData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiTeamActionLogData.getUserId2()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossXingYouShanShuiTeamActionLogData.getParam()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossXingYouShanShuiTeamActionLogData.getReward()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiTeamActionLogData.getLogTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamActionLogData.getEventId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamActionLogData.getDrift()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamActionLogData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamActionLogData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiTeamActionLogData.getId()));
        if (getDb().executeNoneQuery(updateCrossXingYouShanShuiTeamActionLogDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossXingYouShanShuiTeamActionLogDataSql = "insert into `t_c_xing_you_team_action_log_data`(activityId,groupId,id,teamId,logType,userId,userId2,param,reward,logTime,eventId,drift) values (?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossXingYouShanShuiTeamActionLogData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTeamId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLogType()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId2()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getParam()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getReward()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLogTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEventId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrift()));
        if (getDb().executeNoneQuery(insertCrossXingYouShanShuiTeamActionLogDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossXingYouShanShuiTeamActionLogData parseData(ResultSet rs) throws SQLException {
        CrossXingYouShanShuiTeamActionLogData data = new CrossXingYouShanShuiTeamActionLogData();
        data.setActivityId(rs.getInt("activityId"));
        data.setId(rs.getLong("id"));
        data.setTeamId(rs.getInt("teamId"));
        data.setLogType(rs.getInt("logType"));
        data.setUserId(rs.getLong("userId"));
        data.setUserId2(rs.getLong("userId2"));
        data.setParam(rs.getString("param"));
        data.setReward(rs.getString("reward"));
        data.setLogTime(rs.getLong("logTime"));
        data.setEventId(rs.getInt("eventId"));
        data.setDrift(rs.getInt("drift"));
        data.setGroupId(rs.getInt("groupId"));
        return data;
    }
}
